package com.scqkxx.gamebox.activity.game.presenter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.scqkxx.gamebox.activity.game.GameGridView;
import com.scqkxx.gamebox.activity.game.view.GameView;
import com.scqkzqtz.base.library.utils.ToastUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2018/8/13. 10点33分
 * name : ts
 * 用途：游戏页面的控制器
 */

public class GamePresenter {

    private Context mContext; //上下文对象
    private GameView mGameView ; //回调接口数据


    public GamePresenter(Context mContext, GameView mGameView) {
        this.mContext = mContext;
        this.mGameView = mGameView;
    }

    /**
     * 进行网络请求的活动
     */
    public void initGameData(){
        AVQuery<AVObject> query = AVQuery.getQuery("allGameInfo");
        query.findInBackground( new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e ) {
                if (mGameView != null) {
                    mGameView.onFinishFreshAndLoad();
                    if ( e == null ) {
                        Log.e("TAG" ,list.toString());
                        list.get(0).put("imageUrl" , null);
                        list.get(1).put("imageUrl" , null);
                        mGameView.clearActiveList(); //清除数据
                        if ( list.size() > 0 &&  !list.isEmpty() ){
                           mGameView.onGameAllList(list);
                           mGameView.notifyDataSetChanged();
//                           //释放掉缓存
                        }else{
                            mGameView.setEmptyStateView();
                        }
                    } else {
                        if(mGameView.getListSize() <= 0){
                            mGameView.setErrorStateView();
                        } else {
                            mGameView.onActiveFail(e);
                        }
                    }
                }
            }
        });
    }

}
